package com.feng.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author feng
 * @decrition
 * @date 2022/11/322:59
 */
public class L46 {
    public static List<List<Integer>> permute(int[] nums) {
        List<String> result = new ArrayList<>();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            getResult(num, list, result);
            list = result;
            result = new ArrayList<>();
        }
        Collections.sort(list);
        List<List<Integer>> arrayList = new ArrayList<>();
        list.stream().forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                List<Integer> integerList = new ArrayList<>();
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '-') {
                        integerList.add(Integer.valueOf(s.charAt(i) + "" + s.charAt(++i)));
                        continue;
                    }
                    integerList.add(Integer.valueOf(s.charAt(i) + ""));
                }
                arrayList.add(integerList);
            }
        });
        return arrayList;
    }

    private static void getResult(int num, List<String> list, List<String> result) {
        if (list.isEmpty()) {
            String str = num + "";
            result.add(str);
            return;
        }
        for (String integers : list) {
            for (int i = 0; i <= integers.length(); i++) {
                StringBuilder temp = new StringBuilder(integers);
                temp.insert(i, num);
                result.add(temp.toString());
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {0,-1,1};
        List<List<Integer>> permute = permute(nums);
        System.out.println(permute);
    }
}
